package com.songmao.pricecloudapitest.testcase.quote;

import com.songmao.pricecloudapitest.requestcore.ResponePoJo;
import com.songmao.pricecloudapitest.server.PriceCloudHttpCore;
import com.songmao.pricecloudapitest.utils.AssertCore;
import com.songmao.pricecloudapitest.utils.ParsingJson;
import com.songmao.pricecloudapitest.utils.TestData;
import com.songmao.pricecloudapitest.utils.TestUitl;
import io.qameta.allure.Description;
import io.qameta.allure.Step;
import org.testng.ISuite;
import org.testng.Reporter;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author Remy
 * @description : 报价源管理
 * @date 2020-05-09 14:26
 */

public class QuotationSourceManagement extends PriceCloudHttpCore {

    private List<String> idList = new ArrayList<>();


    @Description("报价源管理")
    @Step("报价源管理")
    @Test(dataProvider = "dataProviderMap", dataProviderClass = TestData.class, description = "新增有效报价源")
    public void addMaker(Map<String, String> map) {
        ResponePoJo rs = super.post(map.get("path"), map.get("json"));
        AssertCore.assertEquals(rs.getCode(), map.get("expected"));
        String json = rs.getData().toString();
        AssertCore.assertTrue(Objects.requireNonNull(ParsingJson.getPathCode(json, map.get("saveID"))).toString() != null);
        idList.add(Objects.requireNonNull(ParsingJson.getPathCode(json, map.get("saveID"))).toString());

        ISuite suite = Reporter.getCurrentTestResult().getTestContext().getSuite();
        suite.setAttribute("id", idList);

    }

    @Description("报价源管理")
    @Step("报价源管理")
    @Test(dataProvider = "dataProviderMap", dataProviderClass = TestData.class, description = "新增无效报价源", priority = 1)
    public void addFailureMaker(Map<String, String> map) {
        ResponePoJo rs = super.post(map.get("path"), map.get("json"));
        AssertCore.assertEquals(rs.getCode(), map.get("expected"));
        AssertCore.assertEquals(map.get("twoexpected"), Objects.requireNonNull(ParsingJson.getPathCode(rs.getData().toString(), map.get("assertpath"))).toString()
        );
    }

    @Description("报价源管理")
    @Step("报价源管理")
    @Test(dataProvider = "dataProviderMap", dataProviderClass = TestData.class, description = "编辑报价源", priority = 2)
    public void editUserMaker(Map<String, String> map) {
        ResponePoJo rs = put(map.get("path") + idList.get(TestUitl.random(idList.size())), map.get("json"));
        AssertCore.assertEquals(rs.getCode(), map.get("expected"));
        String json = rs.getData().toString();
        AssertCore.assertEquals(Objects.requireNonNull(ParsingJson.getPathCode(json, map.get("assertpath"))).toString(),
                map.get("twoexpected"));
    }


    @Description("报价源管理")
    @Step("报价源管理")
    @Test(dataProvider = "dataProviderMap", dataProviderClass = TestData.class, description = "根据状态查询报价源", priority = 3)
    public void selectUserStatusMaker(Map<String, String> map) {
        ResponePoJo rs = get(map.get("path"));
        //获取列表下全部enabled
        AssertCore.assertEquals(rs.getCode(), map.get("expected"));
        List<Object> enabledList = ParsingJson.getPathCodes(rs.getData().toString(), map.get("assertpath"));
        for (Object enable : enabledList) {
            AssertCore.assertEquals(enable.toString(), map.get("twoexpected"));
        }

    }

    @Description("报价源管理")
    @Step("报价源管理")
    @Test(dataProvider = "dataProviderMap", dataProviderClass = TestData.class, description = "精准搜索报价源", priority = 4)
    public void selectMaker(Map<String, String> map) {
        ResponePoJo rs = get(map.get("path"));
        AssertCore.assertEquals(map.get("expected"), rs.getCode());

        AssertCore.assertEquals(map.get("twoexpected"), ParsingJson.getPathCode(rs.getData().toString(), map.get("assertpath")).toString());
    }

    @Description("报价源管理")
    @Step("报价源管理")
    @Test(dataProvider = "dataProviderMap", dataProviderClass = TestData.class, description = "根据连接状态查询报价源", priority = 5)
    public void selectConnectStatus(Map<String, String> map) {
        ResponePoJo rs = get(map.get("path"));
        AssertCore.assertEquals(map.get("expected"), rs.getCode());
        List<Object> connectStatusList = ParsingJson.getPathCodes(rs.getData().toString(), map.get("assertpath"));
        for (Object connectStatus : connectStatusList) {
            AssertCore.assertEquals(map.get("twoexpected"), connectStatus.toString());
        }
    }


    @Description("报价源管理")
    @Step("报价源管理")
    @Test(dataProvider = "dataProviderMap", dataProviderClass = TestData.class, description = "删除报价源", priority = 6)
    public void deleteMaker(Map<String, String> map) {
        for (int i = 0; i < idList.size(); i++) {
            String str = idList.get(i);
            ResponePoJo rs = super.delete(map.get("path") + str);
            AssertCore.assertEquals(rs.getCode(), map.get("expected"));
        }
    }


}